home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.016.OffSample / OffSample.p < prev    next >
Encoding:
Text File  |  1989-03-31  |  44.0 KB  |  1,527 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    Offscreen Buffer Sample Application
  6. #
  7. #    OffSample
  8. #
  9. #    OffSample.p        -    Pascal Source
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    
  15. #                1.00                03/89
  16. #
  17. #    Components:
  18. #                OffSample.p            April 1, 1989
  19. #                OffSample.r            April 1, 1989
  20. #                OffSample.h            April 1, 1989
  21. #                POffSample.make        April 1, 1989
  22. #
  23. #    Requirements:
  24. #                Offscreen.p            April 1, 1989
  25. #                Offscreen.inc1.p    April 1, 1989
  26. #                UFailure.p            November 1, 1988
  27. #                UFailure.inc1.p        November 1, 1988
  28. #                UFailure.a            November 1, 1988
  29. #
  30. #    OffSample demonstrates the usage of the Offscreen
  31. #    unit. It shows how to use offscreen pixmaps and
  32. #    bitmaps to produce flicker-free updating with a
  33. #    minimum of re-structuring of code. OffSample attempts
  34. #    to reduce the amount of 'knowledge' that it has of
  35. #    the offscreen structure so as to minimize its
  36. #    dependence on that unit.
  37. #
  38. #    OffSample emphasizes using the Offscreen unit; it
  39. #    is not intended to be viewed as a complete application
  40. #    from which to base some larger effort. Instead, its
  41. #    method of using offscreen bitmaps and pixmaps should
  42. #    be studied and adapted to other applications that
  43. #    desire features such as flicker-free updating.
  44. #
  45. ------------------------------------------------------------------------------}
  46.  
  47.  
  48. PROGRAM OffSample;
  49.  
  50. USES
  51.     MemTypes, QuickDraw, Palettes, OSIntf, ToolIntf,
  52.     PackIntf, Picker, UFailure, Offscreen, Traps;
  53.  
  54. CONST
  55.  
  56.     kSysEnvironsVersion        = 1;
  57.  
  58.     kOSEvent                = app4Evt;        {event used by MultiFinder}
  59.     kSuspendResumeMessage    = 1;            {high byte of suspend/resume event message}
  60.     kResumeMask                = 1;            {bit of msg field for resume vs. suspend}
  61.  
  62.     kMinHeap                = 66 * 1024;
  63.     
  64.     kMinSpace                = 49 * 1024;
  65.     
  66.     kExtremeNeg                = -32768;
  67.     kExtremePos                = 32767 - 1;    {required for old region bug}
  68.     
  69.     sErrStrings                = 128;            {error string STR#}
  70.     eStandardErr            = 1;
  71.     eWrongMachine            = 2;
  72.     eSmallSize                = 3;
  73.     eNoMemory                = 4;
  74.     
  75.     kNoBackBuff                = 128;
  76.     kNoEditBuff                = 129;
  77.     kTitle                    = 130;
  78.     kColorPrompt            = 131;
  79.     kNoWantBack                = 132;
  80.     kNoWantEdit                = 133;
  81.     
  82.     kCMoof                    = 128;
  83.     kGigantor                = 128;
  84.     k1bitGigantor            = 129;
  85.     
  86.     rMenuBar                = 128;            {application's menu bar}
  87.     rAboutAlert                = 128;            {about alert}
  88.     rUserAlert                = 129;            {error user alert}
  89.     rWindow                    = 128;            {application's window}
  90.  
  91.     mApple                    = 128;            {Apple menu}
  92.     iAbout                    = 1;
  93.  
  94.     mFile                    = 129;            {File menu}
  95.     iNew                    = 1;
  96.     iClose                    = 4;
  97.     iQuit                    = 12;
  98.  
  99.     mEdit                    = 130;            {Edit menu}
  100.     iUndo                    = 1;
  101.     iCut                    = 3;
  102.     iCopy                    = 4;
  103.     iPaste                    = 5;
  104.     iClear                    = 6;
  105.     
  106.     mShape                    = 131;            {Shape menu}
  107.     
  108.     mSpecial                = 132;            {Special menu}
  109.     iUseBack                = 1;
  110.     iUseEdit                = 2;
  111.     iPickColor                = 4;
  112.  
  113.     kDITop                    = $0050;
  114.     kDILeft                    = $0070;
  115.     
  116.     kNotDrawn                = -1;
  117.     kLastOne                = -2;
  118.     
  119.     kCursorDepth            = 2;
  120.     kMemoryPolite            = TRUE;
  121.     
  122.     kFramePenH                = 2;
  123.     kFramePenV                = 2;
  124.     
  125.     
  126. TYPE
  127.  
  128.     Shapes            = (kOval, kRegion, kRRect, kPoly, kRect, kICON, kPICT);
  129.  
  130.     ShapeRecord        = RECORD
  131.         next            : Shapes;        {when is it drawn?}
  132.         extent            : Rect;            {where is it?}
  133.     END;
  134.                     
  135.     ShapeArray        = ARRAY [Shapes] OF ShapeRecord;
  136.     
  137.     {An OffscreenRecord contains the WindowRecord for one of our sample windows,
  138.      as well as an offscreen handle for the background and an offscreen handle
  139.      for the background plus the shape being created. It also has an array of
  140.      shapes for this window, a pointer to the first shape, a pointer to the
  141.      shape being edited, and a record of the last state of the buffers. For a
  142.      similar example of extending a toolbox data structure, see how the Window
  143.      Manager and Dialog Manager add fields to the GrafPort and WindowRecord,
  144.      respectively.}
  145.      
  146.     OffscreenRecord    = RECORD
  147.         fWindow        : WindowRecord;        {window data structure for toolbox use}
  148.         fBackHandle    : Handle;            {offscreen pixmap that holds background}
  149.         fEditHandle    : Handle;            {pixmap for background and shape being created}
  150.         fShapes        : ShapeArray;        {the shapes for this window}
  151.         fFirst        : Shapes;            {who is first?}
  152.         fEdit        : Shapes;            {who is being edited?}
  153.         fHasBack    : BOOLEAN;            {did it have a background buffer last time?}
  154.         fHasEdit    : BOOLEAN;            {did it have a edit buffer last time?}
  155.     END;
  156.     OffscreenPeek    = ^OffscreenRecord;
  157.  
  158.  
  159. VAR
  160.     {The "g" prefix is used to emphasize that a variable is global.}
  161.  
  162.     gMac                : SysEnvRec;    {set up by Initialize}
  163.     gHasWaitNextEvent    : BOOLEAN;        {set up by Initialize}
  164.     gInBackground        : BOOLEAN;        {maintained by Initialize and DoEvent}
  165.     
  166.     gShape                : Shapes;        {current shape}
  167.     gUseBack            : BOOLEAN;        {create background offscreen flag}
  168.     gUseEdit            : BOOLEAN;        {create edit offscreen flag}
  169.     gCursor                : CCrsrHandle;    {there can be ONLY one}
  170.     gOughHandle            : Handle;        {offscreen handle for color cursor}
  171.     gPICT                : PicHandle;    {Gigantor}
  172.     gcicn                : CIconHandle;    {Moof!™}
  173.     g1BitHandle            : Handle;        {for the color cursor mask}
  174.  
  175.  
  176. {$S Initialize}
  177. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  178.  
  179. {Check to see if a given trap is implemented. This is only used by the
  180.  Initialize routine in this program, so we put it in the Initialize segment.
  181.  The recommended approach to see if a trap is implemented is to see if
  182.  the address of the trap routine is the same as the address of the
  183.  Unimplemented trap. Needs to be called after call to SysEnvirons so that
  184.  it can check if a ToolTrap is out of range of a pre-MacII ROM.}
  185.  
  186. BEGIN
  187.     IF (tType = ToolTrap) &
  188.         (gMac.machineType > envMachUnknown) &
  189.         (gMac.machineType < envMacII) THEN BEGIN        {it's a 512KE, Plus, or SE}
  190.         tNumber := BAND(tNumber, $03FF);
  191.         IF tNumber > $01FF THEN                            {which means the tool traps}
  192.             tNumber := _Unimplemented;                    {only go to $01FF}
  193.     END;
  194.     TrapAvailable := NGetTrapAddress(tNumber, tType) <>
  195.                         GetTrapAddress(_Unimplemented);
  196. END; {TrapAvailable}
  197.  
  198.  
  199. {$S Main}
  200. PROCEDURE GetGlobalRect (window: WindowPtr; VAR globalRect: Rect);
  201.  
  202. {Return the portRect of window in global coordinates.}
  203.  
  204. VAR
  205.     savePort    : GrafPtr;
  206.     
  207. BEGIN
  208.     GetPort(savePort);
  209.     SetPort(window);                    {so that the correct }
  210.     globalRect := window^.portRect;        { coordinate system is used}
  211.     WITH globalRect DO BEGIN
  212.         LocalToGlobal(topLeft);
  213.         LocalToGlobal(botRight);
  214.     END;
  215.     SetPort(savePort);
  216. END; {GetGlobalRect}
  217.  
  218.  
  219. {$S Main}
  220. FUNCTION IsDAWindow (window: WindowPtr): BOOLEAN;
  221.  
  222. {Check if a window belongs to a desk accessory.}
  223.  
  224. BEGIN
  225.     IF window = NIL THEN
  226.         IsDAWindow := FALSE
  227.     ELSE    {DA windows have negative windowKinds}
  228.         IsDAWindow := WindowPeek(window)^.windowKind < 0;
  229. END; {IsDAWindow}
  230.  
  231.  
  232. {$S Main}
  233. FUNCTION IsAppWindow (window: WindowPtr): BOOLEAN;
  234.  
  235. {Check to see if a window belongs to the application. If the window pointer
  236.  passed was NIL, then it could not be an application window. WindowKinds
  237.  that are negative belong to the system and windowKinds less than userKind
  238.  are reserved by Apple except for windowKinds equal to dialogKind, which
  239.  mean it is a dialog.}
  240.  
  241. BEGIN
  242.     IF window = NIL THEN
  243.         IsAppWindow := FALSE
  244.     ELSE    {application windows have windowKinds >= userKind (8)}
  245.         WITH WindowPeek(window)^ DO
  246.             IsAppWindow := (windowKind = userKind);
  247. END; {IsAppWindow}
  248.  
  249.  
  250. {$S Main}
  251. PROCEDURE FailNILMsg(p: UNIV Ptr; message: INTEGER);
  252.  
  253. {Check for NIL p and fail if so.}
  254.  
  255. BEGIN
  256.     IF p = NIL THEN
  257.         Failure(memFullErr, message);
  258. END; {FailNILMsg}
  259.  
  260.  
  261. {$S Main}
  262. PROCEDURE AlertUser(error: INTEGER; message: LongInt);
  263.  
  264. {Display an alert to inform the user of an error. Message acts as an 
  265.  index into a STR# resource of error messages. If no message is given,
  266.  i.e. = 0, then use a standard message. If error is not noErr then
  267.  display it as well.}
  268.  
  269. VAR
  270.     msg1, msg2    : Str255;
  271.     itemHit        : INTEGER;
  272. BEGIN
  273.     IF message = 0 THEN message := eStandardErr;
  274.     GetIndString(msg1, sErrStrings, message);
  275.     IF error = noErr THEN
  276.         msg2 := ''
  277.     ELSE
  278.         NumToString(error, msg2);
  279.     ParamText(msg1, msg2, '', '');
  280.     itemHit := Alert(rUserAlert, NIL);
  281. END; {AlertUser}
  282.  
  283.  
  284. {$S Main}
  285. FUNCTION DoCloseWindow(window: WindowPtr) : BOOLEAN;
  286.  
  287. {Close a window.}
  288.  
  289. {At this point, if there was a document associated with a
  290.  window, you could do any document saving processing if it is 'dirty'.
  291.  DoCloseWindow would return TRUE if the window actually closes, i.e.,
  292.  the user does not cancel from a save dialog. This result is handy when
  293.  the user quits an application, but then cancels a save of a document
  294.  associated with a window. We also added code to close the application
  295.  window since otherwise, the termination routines would never stop looping,
  296.  waiting for FrontWindow to return NIL.}
  297.  
  298. VAR
  299.     pal    : PaletteHandle;
  300.  
  301. BEGIN
  302.     DoCloseWindow := TRUE;
  303.     IF IsDAWindow(window) THEN
  304.         CloseDeskAcc(WindowPeek(window)^.windowKind);
  305.     IF IsAppWindow(window) THEN BEGIN
  306.         WITH OffscreenPeek(window)^ DO BEGIN
  307.             DisposeOffscreen(fBackHandle);
  308.             DisposeOffscreen(fEditHandle);
  309.         END;
  310.         IF gMac.hasColorQD THEN BEGIN
  311.             pal := GetPalette(window);            {We must handle this ourselves,}
  312.             DisposePalette(pal);                {since we may have done a GetPalette.}
  313.         END;
  314.         CloseWindow(window);                    {Since we provided our own storage.}
  315.         DisposPtr(Ptr(window));
  316.     END;
  317. END; {DoCloseWindow}
  318.  
  319.  
  320. {$S Main}
  321. PROCEDURE EfficientConcat2 (VAR string1, string2: Str255);
  322.  
  323. {Do a more efficient concat than CONCAT since we know
  324.  there are only two strings.}
  325.  
  326. VAR
  327.     len1, len2    : INTEGER;
  328.     
  329. BEGIN
  330.     len1 := LENGTH(string1);
  331.     IF len1 < 255 THEN BEGIN
  332.         len2 := LENGTH(string2);
  333.         IF len1 + len2 > 255 THEN
  334.             len2 := 255 - len1;
  335.         BlockMove(@string2[1], @string1[1 + len1], len2);
  336.         string1[0] := CHR(len1 + len2);
  337.     END;
  338. END; {EfficientConcat2}
  339.  
  340.  
  341. {$S Main}
  342. PROCEDURE AppendTitle (VAR title: Str255; id: INTEGER);
  343.  
  344. {Append the specified string resource data to the provided
  345.  string.}
  346.  
  347.  
  348. VAR
  349.     aString    : StringHandle;
  350.     
  351. BEGIN
  352.     aString := GetString(id);
  353.     IF aString <> NIL THEN BEGIN
  354.         HLock(Handle(aString));                {in case EfficientConcat2 is}
  355.         EfficientConcat2(title, aString^^);
  356.         HUnlock(Handle(aString));            {in a different segment}
  357.     END;
  358. END; {AppendTitle}
  359.  
  360.  
  361. {$S Main}
  362. PROCEDURE CheckTitle (window: WindowPtr; doCheck: BOOLEAN);
  363.  
  364. {Compare the prior state of the offscreen handles for
  365.  window and change its title to reflect the new state.}
  366.  
  367. VAR
  368.     aString                : StringHandle;
  369.     title                : Str255;
  370.     hasBack, hasEdit    : BOOLEAN;
  371.  
  372. BEGIN
  373.     IF IsAppWindow(window) THEN
  374.         WITH OffscreenPeek(window)^ DO BEGIN
  375.             hasBack := (GetMap(fBackHandle) <> NIL);
  376.             hasEdit := (GetMap(fEditHandle) <> NIL);
  377.             IF (NOT doCheck) |                    {set title regardless}
  378.             (fHasBack <> hasBack) |                {or if change}
  379.             (fHasEdit <> hasEdit) THEN BEGIN    {in buffers}
  380.                 fHasBack := hasBack;
  381.                 fHasEdit := hasEdit;
  382.                 title := '';
  383.                 aString := GetString(kTitle);
  384.                 IF aString <> NIL THEN
  385.                     title := aString^^;
  386.                     
  387.                 {If an offscreen handle is NIL, it means
  388.                  that the creation of that offscreen handle
  389.                  was disabled by the user. Once that is
  390.                  done, the buffer will never be created.}
  391.                 
  392.                 IF fBackHandle = NIL THEN
  393.                     AppendTitle(title, kNoWantBack)
  394.                 ELSE IF NOT hasBack THEN
  395.                     AppendTitle(title, kNoBackBuff);
  396.                 IF fEditHandle = NIL THEN
  397.                     AppendTitle(title, kNoWantEdit)
  398.                 ELSE IF NOT hasEdit THEN
  399.                     AppendTitle(title, kNoEditBuff);
  400.                 SetWTitle(window, title);
  401.             END;
  402.         END;
  403. END; {CheckTitle}
  404.  
  405.  
  406. {$S Main}
  407. PROCEDURE DrawShape (shape: Shapes; VAR extent: Rect);
  408.  
  409. {Draw the shape specified in the extent. Extent is a VAR
  410.  parameter because the region and polygon are generated
  411.  from the extent rect and the calculations might result
  412.  in a final shape larger than the original extent.}
  413.  
  414.     PROCEDURE DoRegion;
  415.     
  416.     {Generate a region based on the extent.}
  417.     
  418.     VAR
  419.         r        : Rect;
  420.         rHandle    : RgnHandle;
  421.         pHandle    : PolyHandle;
  422.         
  423.     BEGIN
  424.         r := extent;
  425.         rHandle := NewRgn;
  426.         OpenRgn;
  427.         
  428.         FrameRect(extent);
  429.         WITH r DO BEGIN
  430.             top := top + ((bottom - top) DIV 3);
  431.             bottom := top + ((bottom - top) DIV 2);
  432.         END;
  433.         FrameOval(r);
  434.         pHandle := OpenPoly;
  435.         WITH extent DO BEGIN
  436.             MoveTo(left, top);
  437.             LineTo(right, bottom);
  438.             LineTo(left + (right - left) DIV 2, bottom - (bottom - top) DIV 3);
  439.             LineTo(left, top);
  440.         END;
  441.         ClosePoly;
  442.         FramePoly(pHandle);
  443.         KillPoly(pHandle);
  444.         
  445.         CloseRgn(rHandle);
  446.         extent := rHandle^^.rgnBBox;        {in case bigger than original rect}
  447.         IF gMac.hasColorQD THEN
  448.             PaintRgn(rHandle)
  449.         ELSE
  450.             FillRgn(rHandle, black);
  451.         ForeColor(blackColor);
  452.         FrameRgn(rHandle);
  453.         DisposeRgn(rHandle);
  454.     END; {DoRegion}
  455.     
  456.     PROCEDURE DoPoly;
  457.     
  458.     {Generate a polygon based on the extent.}
  459.     
  460.     VAR
  461.         pHandle    : PolyHandle;
  462.         
  463.     BEGIN
  464.         pHandle := OpenPoly;
  465.         WITH extent DO BEGIN
  466.             MoveTo(left + (right - left) DIV 2, top);
  467.             LineTo(right, bottom);
  468.             LineTo(left, top + (bottom - top) DIV 3);
  469.             LineTo(right, top + (bottom - top) DIV 3);
  470.             LineTo(left, bottom);
  471.             LineTo(left + (right - left) DIV 2, top);
  472.         END;
  473.         ClosePoly;
  474.         extent := pHandle^^.polyBBox;        {in case bigger than original rect}
  475.         IF gMac.hasColorQD THEN
  476.             PaintPoly(pHandle)
  477.         ELSE
  478.             FillPoly(pHandle, ltGray);
  479.         ForeColor(blackColor);
  480.         FramePoly(pHandle);
  481.         KillPoly(pHandle);
  482.     END; {DoPoly}
  483.     
  484. BEGIN
  485.     PenNormal;
  486.     PenSize(kFramePenH, kFramePenV);
  487.     CASE shape OF
  488.         kOval: BEGIN
  489.             IF gMac.hasColorQD THEN
  490.                 PaintOval(extent)
  491.             ELSE
  492.                 FillOval(extent, white);
  493.             ForeColor(blackColor);
  494.             FrameOval(extent);
  495.         END;
  496.         kRegion:
  497.             DoRegion;
  498.         kRRect: BEGIN
  499.             IF gMac.hasColorQD THEN
  500.                 PaintRoundRect(extent, 16, 16)
  501.             ELSE
  502.                 FillRoundRect(extent, 16, 16, gray);
  503.             ForeColor(blackColor);
  504.             FrameRoundRect(extent, 16, 16);
  505.         END;
  506.         kPoly:
  507.             DoPoly;
  508.         kRect: BEGIN
  509.             IF gMac.hasColorQD THEN
  510.                 PaintRect(extent)
  511.             ELSE
  512.                 FillRect(extent, dkGray);
  513.             ForeColor(blackColor);
  514.             FrameRect(extent);
  515.         END;
  516.         kICON:
  517.             IF gMac.hasColorQD THEN
  518.                 PlotCIcon(extent, gcicn)
  519.             ELSE BEGIN 
  520.                 HLock(Handle(gcicn));
  521.                 WITH gcicn^^ DO BEGIN
  522.                 {We cannot call PlotCIcon when Color QD is not
  523.                  present, but we can still use the color icon
  524.                  data.}
  525.                     iconMask.baseAddr := @iconMaskData;
  526.                     iconBMap.baseAddr := Ptr(ORD(@iconMaskData) + 128);
  527.                     CopyMask(iconBMap, iconMask, thePort^.portBits,
  528.                                 iconBMap.bounds, iconMask.bounds, extent);
  529.                 END;
  530.                 HUnlock(Handle(gcicn));
  531.             END;
  532.         kPICT:
  533.             DrawPicture(gPICT, extent);
  534.     END;
  535. END; {DrawShape}
  536.  
  537.  
  538. {$S Main}
  539. FUNCTION GimmeBlackAndWhite(rgb: RGBColor; VAR position: LONGINT): BOOLEAN;
  540.  
  541. {This is a search proc that returns white only if the color is really white;
  542.  otherwise it returns black. It is used to generate the mask for the color
  543.  cursor. It boldly assumes that it is being called for a 1 bit deep map.}
  544.  
  545. BEGIN
  546.     WITH rgb DO
  547.         IF (red = $FFFF) & (green = $FFFF) & (blue = $FFFF) THEN
  548.             position := 0                    {return white if it’s white}
  549.         ELSE
  550.             position := 1;                    {else return black for all other colors}
  551.     GimmeBlackAndWhite := TRUE;
  552. END; {GimmeBlackAndWhite}
  553.  
  554.  
  555. {$S Main}
  556. PROCEDURE SetObjCursor (window: WindowPtr);
  557.  
  558. {Build the color cursor. Note that this routine is only called
  559.  in a Color QD environment, so it doesn't have to make the
  560.  check. Note also that the cursors could have all been 'pre-
  561.  built', thus making things more efficient, but this example
  562.  shows that dynamic cursors can be implemented via pixmaps.}
  563.  
  564. VAR
  565.     colors            : CTabHandle;
  566.     pal                : PaletteHandle;
  567.     rgb                : RGBColor;
  568.     bounder, extent    : Rect;
  569.     buffNotNeeded    : BOOLEAN;
  570.     naughtyBits        : BitMap;
  571.     oneBitPMap        : BitMapPtr;
  572.  
  573. BEGIN
  574.     SetRect(bounder, 0, 0, 16, 16);
  575.     pal := GetPalette(window);
  576.     GetEntryColor(pal, ORD(gShape) + 2, rgb);    {get the color used for the shape}
  577.     
  578.     DisposeOffscreen(gOughHandle);                {get rid of old color table}
  579.     colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  580.     FailNILMsg(colors, eNoMemory);
  581.     colors^^.ctTable[0].rgb := rgb;                {stuff in the color we want}
  582.     
  583.     FailOSErr(NewOffscreen(bounder, kCursorDepth, colors,
  584.                     NOT kMemoryPolite, buffNotNeeded,
  585.                     gOughHandle));
  586.     DisposHandle(Handle(colors));
  587.     
  588.     HLock(Handle(gCursor));
  589.     WITH gCursor^^ DO BEGIN
  590.         crsrMap := PixMapHandle(RecoverHandle(Ptr(GetMap(gOughHandle))));
  591.         crsrData := GetBitsHandle(gOughHandle);
  592.         IF crsrData = NIL THEN BEGIN            {no handle to bits available-punt}
  593.             SetCursor(arrow);
  594.             Exit(SetObjCursor);
  595.         END;
  596.         BeginOffscreenDrawing(gOughHandle, NIL);
  597.         IF NOT (gShape IN [kICON, kPICT]) THEN BEGIN
  598.             SetPt(crsrHotSpot, 0, 0);
  599.             RGBForeColor(rgb);
  600.             extent := bounder;
  601.             InsetRect(extent, 3, 1);            {squeeze it a bit}
  602.             DrawShape(gShape, extent);            {draw the cursor shape}
  603.             PenNormal;
  604.             ForeColor(blackColor);                {draw hot spot}
  605.             MoveTo(0, 0);
  606.             LineTo(0, 1);
  607.         END ELSE BEGIN                            {use a plain cursor for icon/pict}
  608.             SetPt(crsrHotSpot, 2, 2);
  609.             PenNormal;
  610.             ForeColor(blackColor);
  611.             MoveTo(0, 0);
  612.             LineTo(4, 4);
  613.             MoveTo(4, 0);
  614.             LineTo(0, 4);
  615.         END;
  616.         EndOffscreenDrawing(gOughHandle);
  617.         WITH naughtyBits DO BEGIN                {build 1-bit image and mask}
  618.             bounds := bounder;
  619.             baseAddr := @crsr1Data;
  620.             rowBytes := 2;
  621.             CopyBits(BitMapPtr(crsrMap^)^, naughtyBits,
  622.                         bounder, bounder, srcCopy, NIL);
  623.                         
  624.             oneBitPMap :=  GetMap(g1BitHandle);
  625.             oneBitPMap^.baseAddr := @crsrMask;
  626.             AddSearch(@GimmeBlackAndWhite);
  627.             CopyBits(BitMapPtr(crsrMap^)^, oneBitPMap^,
  628.                         bounder, bounder, srcCopy, NIL);
  629.             DelSearch(@GimmeBlackAndWhite);
  630.         END;
  631.         crsrXValid := 0;
  632.         crsrID := GetCTSeed;
  633.     END;
  634.     HUnlock(Handle(gCursor));
  635. END; {SetObjCursor}
  636.  
  637.  
  638. {$S Main}
  639. FUNCTION GetInvalExtent (window: WindowPtr; shape: Shapes) : Rect;
  640.  
  641. {Return the shape's extent, adjusted for the pensize of the frame.}
  642.  
  643. VAR
  644.     r    : Rect;
  645.     
  646. BEGIN
  647.     r := OffscreenPeek(window)^.fShapes[shape].extent;
  648.     WITH r DO BEGIN
  649.         right := right + kFramePenH;
  650.         bottom := bottom + kFramePenV;
  651.     END;
  652.     GetInvalExtent := r;
  653. END; {GetInvalExtent}
  654.  
  655.  
  656. {$S Main}
  657. PROCEDURE ChangeColor (window: WindowPtr);
  658.  
  659. {Display the Color Picker dialog. Note that this is
  660.  only called in Color QD environments.}
  661.  
  662. VAR
  663.     pal                    : PaletteHandle;
  664.     inColor, outColor    : RGBColor;
  665.     where                : Point;
  666.     r                    : Rect;
  667.     aString                : StringHandle;
  668.     prompt                : Str255;
  669.  
  670. BEGIN
  671.     pal := GetPalette(window);
  672.     WITH OffscreenPeek(window)^ DO BEGIN
  673.         GetEntryColor(pal, ORD(gShape) + 2, inColor);
  674.         SetPt(where, kDILeft, kDITop);
  675.         aString := GetString(kColorPrompt);
  676.         IF aString <> NIL THEN
  677.             prompt := aString^^
  678.         ELSE
  679.             prompt := '';
  680.         IF GetColor(where, prompt, inColor, outColor) THEN BEGIN
  681.             SetEntryColor(pal, ORD(gShape) + 2, outColor);
  682.             ActivatePalette(window);
  683.             SetObjCursor(window);
  684.             IF NOT (fShapes[gShape].next = Shapes(kNotDrawn)) THEN BEGIN
  685.                 r := GetInvalExtent(window, gShape);
  686.                 SetPort(window);
  687.                 InvalRect(r);
  688.             END;
  689.         END;
  690.     END;
  691. END; {ChangeColor}
  692.  
  693.  
  694. {$S Main}
  695. PROCEDURE DoNewWindow;
  696.  
  697. {We will allocate our own window storage instead of letting the Window
  698.  Manager do it for two reasons. One, GetNewWindow locks the 'WIND' resource
  699.  handle before calling NewWindow and this can lead to heap fragmentation
  700.  in low memory situations. Two, it takes just as much time for NewWindow
  701.  to get the memory as it does for us to get it. Three, there are THREE
  702.  reasons we will allocate our own window storage instead of letting the
  703.  Window Manager do it. One, GetNewWindow locks etc. etc. Two, it takes
  704.  just as much time, etc. etc. And three, this way we can allocate larger records
  705.  where the extra space can be used to connect other, related data structures.
  706.  Four, there is no fourth reason.}
  707.  
  708. VAR
  709.     p                : Ptr;
  710.     window            : WindowPtr;
  711.     noBuffsPlease    : BOOLEAN;
  712.     title            : Str255;
  713.     shape            : Shapes;
  714.     emptyRect        : Rect;
  715.  
  716. BEGIN
  717.     p := NewPtr(SIZEOF(OffscreenRecord));
  718.     FailNILMsg(p, eNoMemory);
  719.     window := NIL;
  720.     IF gMac.hasColorQD THEN
  721.         window := GetNewCWindow(rWindow, p, WindowPtr(-1))
  722.     ELSE
  723.         window := GetNewWindow(rWindow, p, WindowPtr(-1));
  724.     FailNILMsg(window, eNoMemory);
  725.     
  726.     WITH OffscreenPeek(window)^ DO BEGIN
  727.         fBackHandle := NIL;
  728.         fEditHandle := NIL;
  729.         fHasBack := FALSE;
  730.         fHasEdit := FALSE;
  731.         IF gUseBack THEN
  732.             IF NewOffscreenForWindow(window, noBuffsPlease, fBackHandle) = noErr THEN;
  733.         IF gUseEdit THEN
  734.             IF NewOffscreenForWindow(window, noBuffsPlease, fEditHandle) = noErr THEN;
  735.         SetRect(emptyRect, 0, 0, 0, 0);
  736.         FOR shape := kOval TO kPICT DO BEGIN
  737.             fShapes[shape].next := Shapes(kNotDrawn);
  738.             fShapes[shape].extent := emptyRect;
  739.         END;
  740.         fFirst := Shapes(kNotDrawn);
  741.         fEdit := Shapes(kNotDrawn);
  742.     END;
  743.     
  744.     CheckTitle(window, FALSE);
  745.     IF gMac.hasColorQD THEN
  746.         SetObjCursor(window);
  747. END; {DoNewWindow}
  748.  
  749.  
  750. {$S Initialize}
  751. PROCEDURE Initialize;
  752.  
  753. {Set up the whole world, including global variables, Toolbox managers,
  754.  and menus. We also create one application window at this time.
  755.  Since window storage is non-relocateable, how and when to allocate space
  756.  for windows is very important so that heap fragmentation does not occur.
  757.  Window storage can differ widely amongst applications depending on how many
  758.  windows are created and disposed. If a failure occurs here, we will consider
  759.  that the application is in such bad shape that we should just exit. Your error
  760.  handling may differ, but the checks should still be made.}
  761.  
  762. TYPE
  763.     crsColors = ARRAY[0..2] OF ColorSpec;
  764.  
  765. VAR
  766.     menuBar            : Handle;
  767.     ignoreError        : OSErr;
  768.     total, contig    : LongInt;
  769.     ignoreResult    : BOOLEAN;
  770.     event            : EventRecord;
  771.     count            : INTEGER;
  772.     fi                : FailInfo;
  773.     colors            : CTabHandle;
  774.     bounder            : Rect;
  775.     buffNotNeeded    : BOOLEAN;
  776.  
  777.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  778.     BEGIN
  779.         IF error > 0 THEN
  780.             AlertUser(0, error)
  781.         ELSE
  782.             AlertUser(error, message);
  783.         ExitToShell;
  784.     END; {HandleErr}
  785.  
  786. BEGIN
  787.     gInBackground := FALSE;
  788.  
  789.     InitGraf(@thePort);
  790.     InitFonts;
  791.     InitWindows;
  792.     InitMenus;
  793.     TEInit;
  794.     InitDialogs(NIL);
  795.     InitCursor;
  796.     
  797.     InitOffscreen;
  798.  
  799.     {Call OpenDriver('.MPP', refnum) at this point to initialize AppleTalk,
  800.      if you are using it.}
  801.      
  802.     {NOTE -- It is no longer necessary, and actually unhealthy, to check
  803.      PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  804.      of checking for port availability themselves.}
  805.     
  806.     {This next bit of code is necessary to allow the default button of our
  807.      alert to be outlined.}
  808.      
  809.     FOR count := 1 TO 3 DO
  810.         ignoreResult := EventAvail(everyEvent, event);
  811.  
  812.     CatchFailures(fi, HandleErr);
  813.  
  814.     {Ignore the error returned from SysEnvirons; even if an error occurred,
  815.      the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  816.      call to SysEnvirons by calling it after initializing AppleTalk.}
  817.      
  818.     ignoreError := SysEnvirons(kSysEnvironsVersion, gMac);
  819.     
  820.     {Make sure that the machine has at least 128K ROMs. If it doesn't, exit.}
  821.     
  822.     IF gMac.machineType < 0 THEN
  823.         Failure(0, eWrongMachine);
  824.     
  825.     {Move TrapAvailable call to after SysEnvirons so that we can tell
  826.      in TrapAvailable if a tool trap value is out of range.}
  827.      
  828.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  829.  
  830.     {First check the size of the application heap against a value
  831.      that you have determined is the smallest heap the application can reasonably
  832.      work in. This number should be derived by examining the size of the heap that
  833.      is actually provided by MultiFinder when the minimum size requested is used.
  834.      The derivation of the minimum size requested from MultiFinder is described
  835.      in Sample.h. The check should be made because the preferred size can end up
  836.      being set smaller than the minimum size by the user. This extra check acts to
  837.      insure that your application is starting from a solid memory foundation.}
  838.      
  839.     IF ORD(GetApplLimit) - ORD(ApplicZone) < kMinHeap THEN
  840.         Failure(0, eSmallSize);
  841.     
  842.     {Next, make sure that enough memory is free for your application to run. It
  843.      is possible for a situation to arise where the heap may have been of required
  844.      size, but a large scrap was loaded which left too little memory. To check for
  845.      this, call PurgeSpace and compare the result with a value that you have determined
  846.      is the minimum amount of free memory your application needs at initialization.
  847.      This number can be derived several different ways. One way that is fairly
  848.      straightforward is to run the application in the minimum size configuration
  849.      as described previously. Call PurgeSpace at initialization and examine the value
  850.      returned. However, you should make sure that this result is not being modified
  851.      by the scrap's presence. You can do that by calling ZeroScrap before calling
  852.      PurgeSpace. Make sure to remove that call before shipping, though.}
  853.      
  854.     PurgeSpace(total, contig);
  855.     IF total < kMinSpace THEN
  856.         Failure(0, eNoMemory);
  857.  
  858.     {The extra benefit to waitng until after the Toolbox Managers have been initialized
  859.      before checking memory is that we can now give the user an alert to tell him what
  860.      happened. Although it is possible that the memory situation could be worsened by
  861.      displaying an alert, MultiFinder would gracefully exit the application with
  862.      an informative alert if memory became critical. Here we are acting more
  863.      in a preventative manner to avoid future disaster from low-memory problems.}
  864.  
  865.     menuBar := GetNewMBar(rMenuBar);        {read menus into menu bar}
  866.     FailNILMsg(menuBar, eNoMemory);
  867.     SetMenuBar(menuBar);                    {install menus}
  868.     DisposHandle(menuBar);
  869.     AddResMenu(GetMHandle(mApple), 'DRVR');    {add DA names to Apple menu}
  870.     DrawMenuBar;
  871.     gShape := kOval;
  872.     gUseBack := TRUE;
  873.     gUseEdit := TRUE;
  874.     gOughHandle := NIL;
  875.     
  876.     {Get the 'Moof' icon. If the environment supports Color QD,
  877.      we'll get the color icon. If Color QD is not supported,
  878.      we'll still get the color icon, but use it differently.}
  879.      
  880.     IF gMac.hasColorQD THEN BEGIN
  881.         gcicn := GetCIcon(kCMoof);
  882.         FailNILMsg(gcicn, eNoMemory);
  883.     END ELSE BEGIN
  884.         gcicn := CIconHandle(GetResource('cicn', kCMoof));
  885.         FailNILMsg(gcicn, eNoMemory);
  886.     END;
  887.     
  888.     {If Color QD is supported, we'll get an 8-bit PICT of
  889.      Gigantor. If it isn't supported, we'll get a PICT
  890.      that looks better in non-color ports/}
  891.      
  892.     IF gMac.hasColorQD THEN
  893.         gPICT := GetPicture(kGigantor)
  894.     ELSE
  895.         gPICT := GetPicture(k1bitGigantor);
  896.     FailNILMsg(gPICT, eNoMemory);
  897.     
  898.     {If Color QD is supported, we'll set up a color cursor
  899.      that will be modified later. Otherwise, nothing
  900.      happens. We'll also set up a 1-bit offscreen to
  901.      make a cursor mask.}
  902.      
  903.     IF gMac.hasColorQD THEN BEGIN
  904.         gCursor := CCrsrHandle(NewHandleClear(SIZEOF(CCrsr)));
  905.         FailNILMsg(gCursor, eNoMemory);
  906.         MoveHHi(Handle(gCursor));
  907.         HLock(Handle(gCursor));
  908.         WITH gCursor^^ DO BEGIN
  909.             crsrType := $8001;
  910.             crsrXData := NewHandle(0);
  911.         END;
  912.         HUnlock(Handle(gCursor));
  913.         colors := CTabHandle(NewHandleClear(SIZEOF(ColorTable)));
  914.         FailNILMsg(colors, eNoMemory);
  915.         SetRect(bounder, 0, 0, 16, 16);
  916.         
  917.         {For this one bit deep offscreen guy (used to make cursor masks)
  918.          we pass in a zeroed but otherwise unitialized color table. Since
  919.          the map’s ctable will only have B&W anyway, it doesn’t matter.}
  920.          
  921.         FailOSErr(NewOffscreen(bounder, 1, colors,
  922.                         NOT kMemoryPolite, buffNotNeeded,
  923.                         g1BitHandle));
  924.         DisposHandle(Handle(colors));
  925.     END;
  926.  
  927.     DoNewWindow;                            {create a new window right away}
  928. END; {Initialize}
  929.  
  930.  
  931. {$S Main}
  932. PROCEDURE Terminate;
  933.  
  934. {Clean up the application and exits. We close all of the windows so that
  935.  they can update their documents, if any.
  936.  If we find out that a cancel has occurred, we won't exit to the
  937.  shell, but will return instead.}
  938.  
  939. VAR
  940.     aWindow    : WindowPtr;
  941.     closed    : BOOLEAN;
  942.  
  943. BEGIN
  944.     closed := TRUE;
  945.     REPEAT
  946.         aWindow := FrontWindow;                    {get the current front window}
  947.         IF aWindow <> NIL THEN
  948.             closed := DoCloseWindow(aWindow);    {close this window}
  949.     UNTIL (NOT closed) | (aWindow = NIL);        {do all windows}
  950.     IF closed THEN
  951.         ExitToShell;                            {exit if no cancellation}
  952. END; {Terminate}
  953.  
  954.  
  955. {$S Main}
  956. PROCEDURE AdjustMenus;
  957.  
  958. {Enable and disable menus based on the current state.
  959.  The user can only select enabled menu items. We set up all the menu items
  960.  before calling MenuSelect or MenuKey, since these are the only times that
  961.  a menu item can be selected. Note that MenuSelect is also the only time
  962.  the user will see menu items. This approach to deciding what enable/
  963.  disable state a menu item has the advantage of concentrating all the decision-
  964.  making in one routine, as opposed to being spread throughout the application.
  965.  Other application designs may take a different approach that may or may not be
  966.  just as valid.}
  967.  
  968. VAR
  969.     window            : WindowPtr;
  970.     menu            : MenuHandle;
  971.  
  972. BEGIN
  973.     window := FrontWindow;
  974.  
  975.     menu := GetMHandle(mFile);
  976.     IF IsDAWindow(window) |
  977.         IsAppWindow(window) THEN            {we can allow DAs to be closed from the menu}
  978.         EnableItem(menu, iClose)
  979.     ELSE
  980.         DisableItem(menu, iClose);
  981.  
  982.     menu := GetMHandle(mEdit);
  983.     IF IsDAWindow(window) THEN BEGIN        {a desk accessory might need the edit menu}
  984.         EnableItem(menu, iUndo);
  985.         EnableItem(menu, iCut);
  986.         EnableItem(menu, iCopy);
  987.         EnableItem(menu, iPaste);
  988.         EnableItem(menu, iClear);
  989.     END ELSE BEGIN                            {but we know we do not}
  990.         DisableItem(menu, iUndo);
  991.         DisableItem(menu, iCut);
  992.         DisableItem(menu, iCopy);
  993.         DisableItem(menu, iClear);
  994.         DisableItem(menu, iPaste);
  995.     END;
  996.  
  997.     menu := GetMHandle(mSpecial);
  998.     IF gMac.hasColorQD & IsAppWindow(window) THEN
  999.         EnableItem(menu, iPickColor)        {color can change only if we are top}
  1000.     ELSE
  1001.         DisableItem(menu, iPickColor);
  1002. END; {AdjustMenus}
  1003.  
  1004.  
  1005. {$S Main}
  1006. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  1007.  
  1008. {This is called when an item is chosen from the menu bar (after calling
  1009.  MenuSelect or MenuKey). It performs the right operation for each command.
  1010.  It is good to have both the result of MenuSelect and MenuKey go to
  1011.  one routine like this to keep everything organized.}
  1012.  
  1013. VAR
  1014.     menuID            : INTEGER;        {the resource ID of the selected menu}
  1015.     menuItem        : INTEGER;        {the item number of the selected menu}
  1016.     int                : INTEGER;
  1017.     str                : Str255;
  1018.     ignore            : BOOLEAN;
  1019.  
  1020. BEGIN
  1021.     menuID := HiWrd(menuResult);    {use built-ins (for efficiency)...}
  1022.     menuItem := LoWrd(menuResult);    {to get menu item number and menu number}
  1023.     CASE menuID OF
  1024.         mApple:
  1025.             CASE menuItem OF
  1026.                 iAbout:                {bring up alert for About}
  1027.                     int := Alert(rAboutAlert, NIL);
  1028.                 OTHERWISE BEGIN        {all non-About items in this menu are DAs}
  1029.                     GetItem(GetMHandle(mApple), menuItem, str);
  1030.                     int := OpenDeskAcc(str);
  1031.                 END;
  1032.             END;
  1033.         mFile:
  1034.             CASE menuItem OF
  1035.                 iNew:
  1036.                     DoNewWindow;
  1037.                 iClose:
  1038.                     ignore := DoCloseWindow(FrontWindow); {we don't care if cancelled}
  1039.                 iQuit:
  1040.                     Terminate;
  1041.             END;
  1042.         mEdit:                        {call SystemEdit for DA editing & MultiFinder}
  1043.             ignore := SystemEdit(menuItem-1);    {since we don't do any editing}
  1044.         mShape: 
  1045.             IF gShape <> Shapes(menuItem - 1) THEN BEGIN
  1046.                 CheckItem(GetMHandle(mShape), ORD(gShape) + 1, FALSE);
  1047.                 gShape := Shapes(menuItem - 1);        {the shape is the item}
  1048.                 CheckItem(GetMHandle(mShape), ORD(gShape) + 1, TRUE);
  1049.                 IF gMac.hasColorQD & IsAppWindow(FrontWindow) THEN
  1050.                     SetObjCursor(FrontWindow);
  1051.             END;
  1052.         mSpecial:
  1053.             CASE menuItem OF
  1054.                 iUseBack: BEGIN
  1055.                     gUseBack := NOT gUseBack;
  1056.                     CheckItem(GetMHandle(mSpecial), iUseBack, gUseBack);
  1057.                 END;
  1058.                 iUseEdit: BEGIN
  1059.                     gUseEdit := NOT gUseEdit;
  1060.                     CheckItem(GetMHandle(mSpecial), iUseEdit, gUseEdit);
  1061.                 END;
  1062.                 iPickColor:
  1063.                     ChangeColor(FrontWindow);
  1064.             END;
  1065.     END;
  1066.     HiliteMenu(0);                    {unhighlight what MenuSelect (or MenuKey) hilited}
  1067. END; {DoMenuCommand}
  1068.  
  1069.  
  1070. {$S Main}
  1071. PROCEDURE GoThroughShapes (PROCEDURE WhatToDo(shape: Shapes); window: WindowPtr);
  1072.  
  1073. {Go through the list of shapes for window and
  1074.  call WhatToDo, passing the shape we are on
  1075.  each time.}
  1076.  
  1077. VAR
  1078.     theShape    : Shapes;
  1079.     
  1080. BEGIN
  1081.     WITH OffscreenPeek(window)^ DO
  1082.         IF ORD(fFirst) <> kNotDrawn THEN BEGIN
  1083.             theShape := fFirst;
  1084.             REPEAT
  1085.                 WhatToDo(theShape);
  1086.                 theShape := fShapes[theShape].next;
  1087.             UNTIL ORD(theShape) = kLastOne;
  1088.         END;
  1089. END; {GoThroughShapes}
  1090.  
  1091.  
  1092. {$S Main}
  1093. PROCEDURE DrawAllShapes (window: WindowPtr; doEdit: BOOLEAN);
  1094.  
  1095. {Draw either the currently edited shape or all the shapes
  1096.  in the window's list. Called by DrawWindow.}
  1097.  
  1098. VAR
  1099.     area    : Rect;
  1100.     
  1101.     PROCEDURE AndDrawThem (shape: Shapes);
  1102.     
  1103.     VAR
  1104.         r    : Rect;
  1105.         
  1106.     BEGIN
  1107.         WITH OffscreenPeek(window)^ DO
  1108.             IF shape <> fEdit THEN BEGIN
  1109.                 IF SectRect(OffscreenPeek(window)^.fShapes[shape].extent, area, r)
  1110.                     THEN BEGIN
  1111.                     IF gMac.hasColorQD THEN
  1112.                         PmForeColor(ORD(shape) + 2);
  1113.                     DrawShape(shape, OffscreenPeek(window)^.fShapes[shape].extent);
  1114.                 END;
  1115.             END;
  1116.     END;
  1117.  
  1118. BEGIN
  1119.     SetPort(window);
  1120.     IF doEdit THEN BEGIN
  1121.         WITH OffscreenPeek(window)^ DO                {draw edit shape}
  1122.             IF ORD(fEdit) <> kNotDrawn THEN BEGIN
  1123.                 IF gMac.hasColorQD THEN
  1124.                     PmForeColor(ORD(fEdit) + 2);
  1125.                 DrawShape(fEdit, OffscreenPeek(window)^.fShapes[fEdit].extent);
  1126.             END;
  1127.     END ELSE BEGIN
  1128.         area := window^.visRgn^^.rgnBBox;
  1129.         GoThroughShapes(AndDrawThem, window);
  1130.     END;
  1131. END; {DrawAllShapes}
  1132.  
  1133.  
  1134. {$S Main}
  1135. PROCEDURE DrawWindow(window: WindowPtr);
  1136.  
  1137. {The core application window updating routine. Understands about Offscreen
  1138.  setup, (in this case, two nested offscreen buffers), and what needs to
  1139.  be drawn, in this case, a whole bunch of shapes. Called from two routines,
  1140.  DoUpdate and DoContentClick. The way it works is first, by calling
  1141.  BeginUpdateOffscreen on fEditHandle, the drawing is redirected to
  1142.  the 'edit' offscreen pixmap. Next, if any drawing needs to be done
  1143.  in the 'background' pixmap, then by calling BeginOffscreenDrawing on
  1144.  fBackHandle, drawing is further redirected. All the shapes that exist
  1145.  but are not the one being edited (i.e., the background) are drawn here
  1146.  and the EndOffscreenDrawing causes the redirecting to cease. Then the
  1147.  pixmap is copybitsed into the next outer layer of drawing, whether that
  1148.  is the 'edit' offscreen pixmap or the window itself. There, the shape
  1149.  being edited is drawn. Finally, EndUpdateOffscreen is called to cease
  1150.  that layer of redirection and copybits the 'edit' offscreen to the window.
  1151.  The way this is designed, it all still works if either or both of the
  1152.  offscreen pixmaps is missing.}
  1153.  
  1154. VAR
  1155.     globalRect    : Rect;
  1156.     drawNeeded    : BOOLEAN;
  1157.     backMap        : BitMapPtr;
  1158.     
  1159. BEGIN
  1160.     GetGlobalRect(window, globalRect);
  1161.     WITH OffscreenPeek(window)^ DO BEGIN
  1162.         IF CheckBoundsOffscreen(fEditHandle, globalRect, drawNeeded) <> noErr THEN {do nada};
  1163.         SetPort(window);
  1164.         BeginUpdateOffscreen(fEditHandle, window);    {this sets up the visRgn}
  1165.         
  1166.         IF CheckBoundsOffscreen(fBackHandle, globalRect, drawNeeded) <> noErr THEN 
  1167.                                                     {do nada};
  1168.         IF drawNeeded THEN BEGIN                    {draw if updating needs to be done}
  1169.             BeginOffscreenDrawing(fBackHandle, window);
  1170.             EraseRect(window^.portRect);            {clear out any garbage that might}
  1171.             DrawAllShapes(window, FALSE);            {be left behind and draw the}
  1172.             EndOffscreenDrawing(fBackHandle);        {'background'}
  1173.         END;
  1174.         backMap := GetMap(fBackHandle);
  1175.         IF backMap <> NIL THEN BEGIN
  1176.             ForeColor(blackColor);
  1177.             BackColor(whiteColor);                    {so funny colorization doesn't happen}
  1178.             WITH window^ DO BEGIN
  1179.                 CopyBits(backMap^, portBits, portRect, portRect, srcCopy, NIL);
  1180.                 ValidRectOffscreen(fBackHandle, NIL, portRect);
  1181.             END;
  1182.         END;
  1183.         DrawAllShapes(window, TRUE);                {only draw the edited shape}
  1184.         
  1185.         EndUpdateOffscreen(fEditHandle, window);
  1186.         CheckTitle(window, TRUE);                    {buffers may have changed}
  1187.     END;
  1188. END; {DrawWindow}
  1189.  
  1190.  
  1191. {$S Main}
  1192. PROCEDURE DoContentClick (window: WindowPtr; event: EventRecord);
  1193.  
  1194. {This is called when a mouse-down event occurs in the content of a window.
  1195.  Other applications might want to call FindControl, TEClick, etc., to
  1196.  further process the click. In Offsample, a user click in the content
  1197.  region means a shape is to be added or changed.}
  1198.  
  1199. VAR
  1200.     oldRect, newRect        : Rect;
  1201.     anchorPt, oldPt, nextPt    : Point;
  1202.     lastShape                : Shapes;
  1203.     first                    : BOOLEAN;
  1204.     
  1205.     PROCEDURE AndReorderThem (shape: Shapes);
  1206.     
  1207.     {Remove the edited shape from the linked list of shapes.}
  1208.     
  1209.     BEGIN
  1210.         WITH OffscreenPeek(window)^ DO
  1211.             IF shape <> gShape THEN
  1212.                 lastShape := shape
  1213.             ELSE
  1214.                 IF fFirst = shape THEN
  1215.                     fFirst := fShapes[shape].next
  1216.                 ELSE
  1217.                     fShapes[lastShape].next := fShapes[shape].next;
  1218.     END; {AndReorderThem}
  1219.  
  1220. BEGIN
  1221.     IF IsAppWindow(window) THEN
  1222.         WITH OffscreenPeek(window)^ DO BEGIN
  1223.             anchorPt := event.where;
  1224.             GlobalToLocal(anchorPt);
  1225.             oldPt := anchorPt;
  1226.             
  1227.             {If the shape being edited existed previously, we need
  1228.              to invalidate its old position so that it gets
  1229.              'erased'.}
  1230.              
  1231.             IF ORD(fShapes[gShape].next) <> kNotDrawn THEN BEGIN
  1232.                 oldRect := GetInvalExtent(window, gShape);
  1233.                 InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1234.                 InvalRectOffscreen(fEditHandle, window, oldRect);
  1235.             END;
  1236.             fEdit := gShape;                            {flag this shape as edited}
  1237.             lastShape := Shapes(kLastOne);
  1238.             GoThroughShapes(AndReorderThem, window);
  1239.             IF ORD(lastShape) <> kLastOne THEN
  1240.                 fShapes[lastShape].next := gShape        {make edited shape last}
  1241.             ELSE
  1242.                 fFirst := gShape;                        {or if only shape, first}
  1243.             fShapes[gShape].next := Shapes(kLastOne);
  1244.             Pt2Rect(anchorPt, anchorPt, oldRect);
  1245.             first := TRUE;                                {indicate first time though loop}
  1246.             WHILE WaitMouseUp DO BEGIN                    {while the mouse is down…}
  1247.                 GetMouse(nextPt);
  1248.                 IF first | (NOT EqualPt(oldPt, nextPt)) THEN BEGIN
  1249.                     first := FALSE;                        {no longer first time through loop}
  1250.                     oldPt := nextPt;
  1251.                     CASE gShape OF
  1252.                         kOval,                            {build a rectangle for these}
  1253.                         kRegion,                        {from the anchor point and}
  1254.                         kRRect,                            {the current point}
  1255.                         kPoly,
  1256.                         kRect:
  1257.                             Pt2Rect(anchorPt, nextPt, newRect);
  1258.                         kICON:                            {rect from current position}
  1259.                             WITH nextPt, newRect DO BEGIN
  1260.                                 top := v;
  1261.                                 left := h;
  1262.                                 bottom := top + 32;
  1263.                                 right := left + 32;
  1264.                             END;
  1265.                         kPICT:                            {rect from current position}
  1266.                             WITH nextPt, newRect DO BEGIN
  1267.                                 newRect := gPICT^^.picFrame;
  1268.                                 OffsetRect(newRect, -left, -top);
  1269.                                 OffsetRect(newRect, h, v);
  1270.                             END;
  1271.                     END;
  1272.                     fShapes[gShape].extent := newRect;
  1273.                     UnionRect(newRect, oldRect, oldRect);
  1274.                     
  1275.                     {In the case of the 'stretchable' shapes whose extents are
  1276.                      built from the anchor point and the current point, doing
  1277.                      a UnionRect is pretty close to being as efficient as doing
  1278.                      a UnionRgn with two regions that are shaped like the old
  1279.                      and new extents. However, a case can be made for using
  1280.                      regions for the icon and the picture since they move around
  1281.                      instead of 'stretching'. The effect of extra, unnecessary
  1282.                      invalidation is, of course, most noticeable when there is
  1283.                      no edit offscreen and the icon/picture is moved around
  1284.                      rapidly. Changing the code to use regions is LEFT AS AN
  1285.                      EXERCISE FOR THE READER, Ha-Ha-Ha.}
  1286.                      
  1287.                     InvalRectOffscreen(fEditHandle, window, oldRect);
  1288.                     DrawWindow(window);
  1289.                     oldRect := GetInvalExtent(window, gShape);
  1290.                 END;
  1291.             END;
  1292.             fEdit := Shapes(kNotDrawn);
  1293.             oldRect := GetInvalExtent(window, gShape);
  1294.             InvalRectOffscreen(fBackHandle, NIL, oldRect);
  1295.         END;
  1296. END; {DoContentClick}
  1297.  
  1298.  
  1299. {$S Main}
  1300. PROCEDURE DoUpdate(window: WindowPtr);
  1301.  
  1302. {This is called when an update event is received for a window.
  1303.  It calls DrawWindow to draw the contents of an application window.}
  1304.  
  1305. BEGIN
  1306.     IF IsAppWindow(window) THEN
  1307.         DrawWindow(window);
  1308. END; {DoUpdate}
  1309.  
  1310.  
  1311. {$S Main}
  1312. PROCEDURE DoActivate(window: WindowPtr; becomingActive: BOOLEAN);
  1313.  
  1314. {This is called when a window is activated or deactivated.}
  1315.  
  1316. BEGIN
  1317.     IF IsAppWindow(window) THEN
  1318.         IF gMac.hasColorQD & becomingActive THEN
  1319.             SetObjCursor(window);
  1320. END; {DoActivate}
  1321.  
  1322.  
  1323. {$S Main}
  1324. PROCEDURE AdjustCursor(region: RgnHandle);
  1325.  
  1326. {Change the cursor's shape, depending on its position. This also calculates the region
  1327.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  1328.  that region, an event is generated, causing this routine to be called. This
  1329.  allows us to change the region to the region the mouse is currently in. If
  1330.  there is more to the event than just “the mouse moved”, we get called before the
  1331.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  1332.  this is called again before we fall back into WNE.}
  1333.  
  1334. VAR
  1335.     window                : WindowPtr;
  1336.     arrowRgn            : RgnHandle;
  1337.     shapeRgn            : RgnHandle;
  1338.     globalPortRect        : Rect;
  1339.     mouse                : Point;
  1340.  
  1341. BEGIN
  1342.     window := FrontWindow;
  1343.     {we only adjust the cursor when we are in front}
  1344.     IF (NOT gInBackground) AND (NOT IsDAWindow(window)) THEN BEGIN
  1345.         GetMouse(mouse);
  1346.         LocalToGlobal(mouse);
  1347.         
  1348.         {calculate regions for different cursor shapes}
  1349.         arrowRgn := NewRgn;
  1350.         shapeRgn := NewRgn;
  1351.  
  1352.         {start with a big, big rectangular region}
  1353.         SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg,
  1354.                             kExtremePos, kExtremePos);
  1355.  
  1356.         {calculate shapeRgn}
  1357.         IF IsAppWindow(window) THEN BEGIN
  1358.             SetPort(window);            {make a global version of the portRect}
  1359.             IF gMac.hasColorQD THEN
  1360.                 WITH CGrafPtr(window)^ DO
  1361.                     SetOrigin(-portPixMap^^.bounds.left, -portPixMap^^.bounds.top)
  1362.             ELSE
  1363.                 WITH window^.portBits.bounds DO
  1364.                     SetOrigin(-left, -top);
  1365.             globalPortRect := window^.portRect;
  1366.             RectRgn(shapeRgn, globalPortRect);
  1367.             SectRgn(shapeRgn, window^.visRgn, shapeRgn);
  1368.             SetOrigin(0, 0);
  1369.         END;
  1370.  
  1371.         {subtract other regions from arrowRgn}
  1372.         DiffRgn(arrowRgn, shapeRgn, arrowRgn);
  1373.  
  1374.         {change the cursor and the region parameter}
  1375.         IF PtInRgn(mouse, shapeRgn) THEN BEGIN
  1376.             IF gMac.hasColorQD THEN
  1377.                 SetCCursor(gCursor)
  1378.             ELSE
  1379.                 SetCursor(GetCursor(crossCursor)^^);
  1380.             CopyRgn(shapeRgn, region);
  1381.         END ELSE BEGIN
  1382.             SetCursor(arrow);
  1383.             CopyRgn(arrowRgn, region);
  1384.         END;
  1385.  
  1386.         {get rid of our local regions}
  1387.         DisposeRgn(arrowRgn);
  1388.         DisposeRgn(shapeRgn);
  1389.     END;
  1390. END; {AdjustCursor}
  1391.  
  1392.  
  1393. {$S Main}
  1394. PROCEDURE DoEvent(event: EventRecord);
  1395.  
  1396. {Do the right thing for an event. Determine what kind of event it is, and call
  1397.  the appropriate routines.}
  1398.  
  1399. VAR
  1400.     part, err    : INTEGER;
  1401.     window        : WindowPtr;
  1402.     ignore        : BOOLEAN;
  1403.     key            : CHAR;
  1404.     aPoint        : Point;
  1405.     fi            : FailInfo;
  1406.     
  1407.     PROCEDURE HandleErr(error: INTEGER; message: LongInt);
  1408.     BEGIN
  1409.         IF error > 0 THEN
  1410.             AlertUser(0, error)
  1411.         ELSE
  1412.             AlertUser(error, message);
  1413.         EXIT(DoEvent);
  1414.     END; {HandleErr}
  1415.  
  1416. BEGIN
  1417.     CatchFailures(fi, HandleErr);
  1418.     CASE event.what OF
  1419.         mouseDown: BEGIN
  1420.             part := FindWindow(event.where, window);
  1421.             CASE part OF
  1422.                 inMenuBar: BEGIN            {process the menu command}
  1423.                     AdjustMenus;
  1424.                     DoMenuCommand(MenuSelect(event.where));
  1425.                 END;
  1426.                 inSysWindow:                {let the system handle the mouseDown}
  1427.                     SystemClick(event, window);
  1428.                 inContent:
  1429.                     IF window <> FrontWindow THEN BEGIN
  1430.                         SelectWindow(window);
  1431.                         {DoEvent(event);}    {use this line for "do first click"}
  1432.                     END ELSE
  1433.                         DoContentClick(window, event);
  1434.                 inDrag:                        {pass screenBits.bounds to get all gDevices}
  1435.                     DragWindow(window, event.where, screenBits.bounds);
  1436.                 inGrow:;
  1437.                 inZoomIn, inZoomOut:;
  1438.                 inGoAway:
  1439.                     IF TrackGoAway(window, event.where) THEN
  1440.                         ignore := DoCloseWindow(window);
  1441.             END;
  1442.         END;
  1443.         keyDown, autoKey: BEGIN                {check for menukey equivalents}
  1444.             key := CHR(BAnd(event.message, charCodeMask));
  1445.             IF BAnd(event.modifiers, cmdKey) <> 0 THEN    {Command key down}
  1446.                 IF event.what = keyDown THEN BEGIN
  1447.                     AdjustMenus;            {enable/disable/check menu items properly}
  1448.                     DoMenuCommand(MenuKey(key));
  1449.                 END;
  1450.         END;                                {call DoActivate with the window and...}
  1451.         activateEvt:                        {TRUE for activate, FALSE for deactivate}
  1452.             DoActivate(WindowPtr(event.message), BAnd(event.modifiers, activeFlag) <> 0);
  1453.         updateEvt:                          {call DoUpdate with the window to update}
  1454.             DoUpdate(WindowPtr(event.message));
  1455.         diskEvt:
  1456.             IF HiWrd(event.message) <> noErr THEN BEGIN
  1457.                 SetPt(aPoint, kDILeft, kDITop);
  1458.                 err := DIBadMount(aPoint, event.message);
  1459.             END;
  1460.         kOSEvent:
  1461.             CASE BAnd(BRotL(event.message, 8), $FF) OF    {high byte of message}
  1462.                 kSuspendResumeMessage: BEGIN
  1463.                     gInBackground := BAnd(event.message, kResumeMask) = 0;
  1464.                     DoActivate(FrontWindow, NOT gInBackground);
  1465.                 END;
  1466.             END;
  1467.     END;
  1468.     Success(fi);
  1469. END; {DoEvent}
  1470.  
  1471.  
  1472. {$S Main}
  1473. PROCEDURE EventLoop;
  1474.  
  1475. {Get events forever, and handle them by calling DoEvent.
  1476.  Get the events by calling WaitNextEvent, if it's available, otherwise
  1477.  by calling GetNextEvent. Also call AdjustCursor each time through the loop.}
  1478.  
  1479. VAR
  1480.     cursorRgn    : RgnHandle;
  1481.     gotEvent    : BOOLEAN;
  1482.     event        : EventRecord;
  1483.  
  1484. BEGIN
  1485.     cursorRgn := NewRgn;            {we’ll pass WNE an empty region the 1st time thru}
  1486.     REPEAT
  1487.         IF gHasWaitNextEvent THEN {put us 'asleep' forever under MultiFinder}
  1488.             gotEvent := WaitNextEvent(everyEvent, event, MAXLONGINT, cursorRgn)
  1489.         ELSE BEGIN
  1490.             SystemTask;                {must be called if using GetNextEvent}
  1491.             gotEvent := GetNextEvent(everyEvent, event);
  1492.         END;
  1493.         IF gotEvent THEN BEGIN
  1494.             AdjustCursor(cursorRgn); {make sure we have the right cursor}
  1495.             DoEvent(event);
  1496.         END;
  1497.         AdjustCursor(cursorRgn);
  1498.     UNTIL FALSE;                    {loop forever; we quit through an ExitToShell}
  1499. END; {EventLoop}
  1500.  
  1501.  
  1502. PROCEDURE _DataInit; EXTERNAL;
  1503.  
  1504. {This routine is part of the MPW runtime library. This external
  1505.  reference to it is done so that we can unload its segment, %A5Init.}
  1506.  
  1507. {$S Main}
  1508. BEGIN
  1509.     UnloadSeg(@_DataInit);    {note that _DataInit must not be in Main!}
  1510.     
  1511.     MoreMasters;
  1512.     MoreMasters;
  1513.     MoreMasters;            {prepare for handles used by Offscreen}
  1514.     
  1515.     {If you have stack requirements that differ from the default,
  1516.      then you could use SetApplLimit to increase StackSpace at 
  1517.      this point, before calling MaxApplZone.}
  1518.      
  1519.     MaxApplZone;            {expand the heap so code segments load at the top}
  1520.  
  1521.     InitSignals;
  1522.     Initialize;                {initialize the program}
  1523.     UnloadSeg(@Initialize);    {note that Initialize must not be in Main!}
  1524.  
  1525.     EventLoop;                {call the main event loop}
  1526. END.
  1527.